#pragma once
#include "afn0dbase.h"

#pragma pack(1)
class ActiveAfn0df4 : public Afn0dfbase
{
public:
	CommonA20 _time;
public:
	inline ActiveAfn0df4(uint16_t pn=0)
	:Afn0dfbase(4, pn)
	{
	}
	inline uint16_t length(void)const
	{
		return sizeof(*this);
	}
	inline Mapss& get(Mapmss& mss)const
	{
		Mapss& m = mss[(int)pnfn];
		m["Fn"]   = Fn();
		m["Pn"]   = Pn();
		m["_time"] = _time.toStringU();
		return m;
	}
	inline bool set(const Mapss& m)
	{
		if(!m.have("Pn")
		|| !m.have("_time")
		|| !m.equal("Fn", Fn()))
		{
			return false;
		}
		int pn = 0;
		Mapss M = m;
		Pn(M["Pn"].toint());
		M["_time"] >> _time;
		return true;
	}
};

class PasiveAfn0df4 : public Afn0dfbase
{
public:
	enum{RATE_MIN = 1, RATE_MAX = 12};
public:
	CommonA20 _froztime;
	CommonA15 _copytime;
	uint8_t   _rate;
	CommonA23 _rvsttamd;			//reverseTotalActiveMaxDemand;
	CommonA23 _rvsamd	[RATE_MAX];	//reverseActiveMaxDemand[RATE_MAX];
	CommonA17 _rvsttamdt;			//reverseTotalActiveMaxDemandTime;
	CommonA17 _rvsamdt	[RATE_MAX];	//reverseActiveMaxDemandTime[RATE_MAX];
	CommonA23 _rvsttrmd;			//reverseTotalReactiveMaxDemand;
	CommonA23 _rvsrmd	[RATE_MAX];	//reverseReactiveMaxDemand[RATE_MAX];
	CommonA17 _rvsttrmdt;			//reverseTotalReactiveMaxDemandTime;
	CommonA17 _rvsrmdt	[RATE_MAX];	//reverseReactiveMaxDemandTime[RATE_MAX];
public:
	PasiveAfn0df4(uint16_t pn = 0)
	:Afn0dfbase(4, pn),_rate(RATE_MAX)
	{
	}
	inline uint16_t length(void)const
	{
		if(_rate >= RATE_MIN && _rate <= RATE_MAX)
		{
			return sizeof(PasiveAfn0df4)
				 - (RATE_MAX - _rate) * sizeof(CommonA23) 
				 - (RATE_MAX - _rate) * sizeof(CommonA17)
				 - (RATE_MAX - _rate) * sizeof(CommonA23) 
				 - (RATE_MAX - _rate) * sizeof(CommonA17)
				 ;
		}
		return 0;
	}
	inline void rate(uint8_t r)
	{
		if(r >= RATE_MIN && r <= RATE_MAX)
		{
			_rate = r;
		}
	}
	inline Mapss& get(Mapmss& mss)const
	{
		Mapss& m = mss[(int)pnfn];
		return m;
	}
	inline bool set(const Mapss& m)
	{

		if(!m.have("Pn")
		|| !m.equal("Fn", Fn())
		|| !m.have("_froztime")
		|| !m.have("_copytime")
		|| !m.have("_rate")	  
		|| !m.have("_rvsttamd")  
		|| !m.have("_rvsttamdt")  
		|| !m.have("_rvsttrmd") 
		|| !m.have("_rvsttrmdt") )
		{
			cout << __func__ << "," << __LINE__ << endl;
			return false;
		}
		Mapss M = m;

		Pn(M["Pn"].toint());
		M["_froztime" ] >> _froztime;
		M["_copytime" ] >> _copytime;
		M["_rate"	  ] >> _rate;
		M["_rvsttamd" ] >> _rvsttamd;
		M["_rvsttamdt"] >> _rvsttamdt;	
		M["_rvsttrmd" ] >> _rvsttrmd;
		M["_rvsttrmdt"] >> _rvsttrmdt;

		for(int i = 0; i < _rate; i++)
		{
			cout <<  i << endl;

			sstream rvsamd ;
			sstream rvsamdt;
			sstream rvsrmd ;
			sstream rvsrmdt;

			rvsamd  << "_rvsamd_"  << i;
			rvsamdt << "_rvsamdt_" << i;
			rvsrmd  << "_rvsrmd_"  << i;
			rvsrmdt << "_rvsrmdt_" << i;

			M[rvsamd .str()] >> _rvsamd [i];	
			M[rvsamdt.str()] >> _rvsamdt[i];
			M[rvsrmd .str()] >> _rvsrmd [i];
			M[rvsrmdt.str()] >> _rvsrmdt[i];
		}
		cout << hexBySize(_froztime) << endl;
		cout << hexBySize(_copytime) << endl;
		return true;
	}
};
#pragma pack(0)
